package com.leo.lime.music.controller;

import android.content.Context;
import android.os.Handler;
import android.provider.MediaStore;

import android.database.Cursor;

import com.leo.lime.music.content.me.domain.UserInfo;

import java.util.ArrayList;

/**
 * Created by android on 18-3-17.
 */
public class MusicController {
    private static final String TAG = "MusicController";

    public Context mCtx;
    private Handler mHandler;
    private ArrayList<Listener> mListeners;

    private static MusicController mMusicController;

    public interface Listener {
        void updateUi();
    }

    private MusicController(Context ctx) {
        mCtx = ctx;
    }

    public static MusicController getInstance(Context ctx) {
        if(mMusicController == null) {
            mMusicController = new MusicController(ctx.getApplicationContext());
        }
        return mMusicController;
    }

    public void setHandler(Handler handler) {
        mHandler = handler;
    }

    public void addListener(Listener l) {
        if(mListeners == null) {
            mListeners = new ArrayList<Listener>();
        }
        if(!mListeners.contains(l)) {
            mListeners.add(l);
        }
    }

    public void removeListener(Listener l) {
        if(mListeners != null && mListeners.size() > 0) {
            if(mListeners.contains(l)) {
                mListeners.remove(l);
            }
        }
    }

    public void removeListeners() {
        if(mListeners != null && mListeners.size() > 0) {
            for(Listener lt : mListeners) {
                mListeners.remove(lt);
            }
        }
    }

    public void applyListeners() {
        if(mListeners != null && mListeners.size() > 0) {
            for(Listener lt : mListeners) {
                lt.updateUi();
            }
        }
    }

    public void queryMusics() {
        Cursor cursor = null;
        try{
            cursor = mCtx.getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        } catch (Exception e) {

        } finally {
            if(cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
    }

    public boolean isUserLogin() {
        return false;
    }

    public UserInfo getUserInfo() {
        return null;
    }
}
